package mint.modules.generator.expression;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;

import org.antlr.runtime.tree.Tree;

import mint.MintLexer;
import mint.modules.generator.Scope;
import mint.modules.type.Type;

public class LiteralNode extends Expression {
	private String literal;
	private Type type;
	private int line;
	private List<String> errors;

	public LiteralNode(Scope theParent, ExpressionType exprType, Tree literalTree) {
		super(theParent, exprType);
		literal = literalTree.getText();
		if(literalTree.getType() == MintLexer.IntegerLiteral)
			type = Type.intType;
		else if (literalTree.getType() == MintLexer.FloatingPointLiteral)
			type = Type.floatType;
		else if (literalTree.getType() == MintLexer.StringLiteral)
			type = Type.stringType;
		else if (literal.equals("true"))
		{
			type = Type.booleanType;
			literal = "1";
		} else if(literal.equals("false"))
		{
			type = Type.booleanType;
			literal = "0";
		}else if(literal.equals("null"))
		{
			type = Type.objectNullType;
		}
		else
			type = Type.intType;
		line = literalTree.getLine();
		errors = new ArrayList<String>();
	}

	@Override
	public void testSemantic() {
		if (getExpressionType() == ExpressionType.Left || getExpressionType() == ExpressionType.RightLeft)
			errors.add(line + ": literal expression must be on the right side of a assignation");
	}

	@Override
	public List<String> getSemanticErrors() {
		return errors;
	}

	@Override
	public void emit(Formatter output) {
		if (literal.equals("null"))
		{
			output.format("aconst_null\n");
		}
		else
			output.format("ldc %s\n", literal);
	}

	@Override
	public int getLine() {
		return line;
	}

	@Override
	public Type getType() {
		return type;
	}

	@Override
	public void leftEmit(Formatter output) {}

}
